package cn.edu.neusoft.food229.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import android.app.FragmentTransaction;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.material.bottomnavigation.BottomNavigationView;

import java.util.ArrayList;
import java.util.List;

import cn.edu.neusoft.food229.R;
import cn.edu.neusoft.food229.fragment.Fragment1;
import cn.edu.neusoft.food229.fragment.Fragment2;
import cn.edu.neusoft.food229.fragment.Fragment3;
import cn.edu.neusoft.food229.fragment.Fragment4;
import cn.edu.neusoft.food229.fragment.Fragment5;
import cn.edu.neusoft.food229.fragment.FragmentBase_229;

public class MainActivity extends AppCompatActivity {
    private List<FragmentBase_229> fragmentList;
    private ViewPager viewPager;
    private BottomNavigationView bottomnv;
    private FragmentPagerAdapter fragmentPagerAdapter;
//    private FragmentManager fragmentManager;

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {//初始化菜单
        MenuInflater inflater=getMenuInflater();
        inflater.inflate(R.menu.menu,menu);//动态
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {//选择菜单某一条目
        switch (item.getItemId()){
            case R.id.menu1:break;
//            case R.id.app_bar_search:break;
            case R.id.menu2: showmydlg();  break;
            case R.id.menu3: break;
            case R.id.menu4: break;
            case R.id.menu41: break;
            case R.id.menu42: break;


        }
        return super.onOptionsItemSelected(item);
    }
    private void showmydlg() {
       final View view=getLayoutInflater().inflate(R.layout.pinglun,null);
        AlertDialog dlg=new AlertDialog.Builder(this).setTitle("查找排名")
                .setIcon(R.mipmap.ic_launcher).setView(view).setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
//                        TextView tv;
                        EditText et;
//                        tv=view.findViewById(R.id.textView13);
                        et=view.findViewById(R.id.editText1);
//                       tv.getText();
                        Toast.makeText(MainActivity.this,"                "+ et.getText().toString()+"              ", Toast.LENGTH_SHORT).show();
                    }
                }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                    }
                }).create();
        dlg.show();

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initViews();
        initViewPager();
        initBottomNV();
    }

    private void initViews() {
        fragmentList=new ArrayList<>();
        fragmentList.add(new Fragment1());
        fragmentList.add(new Fragment2());
        fragmentList.add(new Fragment3());
        fragmentList.add(new Fragment5());//----购物车
        fragmentList.add(new Fragment4());

//        FragmentTransaction transaction = fragmentManager.findFragmentById(0).beginTransaction();
//        transaction.setTransition(android.support.v4.app.FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
//        transaction.replace(R.id.content, messageFragment);
//        transaction.commit();

    }

    private void initViewPager() {
        viewPager=findViewById(R.id.viewPager);/*实例化viewPager*/
        /*实例化FragmnetPagerAdapter*/
        fragmentPagerAdapter=new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return fragmentList.get(position);/*取出第position个fragment*/
            }/*得到项目*/
            @Override
            public int getCount() {
                return fragmentList.size();
            }
        };/*得到数字*/


        viewPager.setAdapter(fragmentPagerAdapter);/*给一个控件设适配器*/

        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset,
                                       int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                System.out.println("------------position="+position);
                switch (position){
                    case 0:
                        bottomnv.setSelectedItemId(R.id.key);break;
                    case 1:
                        bottomnv.setSelectedItemId(R.id.collect);break;
                    case 2:
                        bottomnv.setSelectedItemId(R.id.search);break;
                    case 3:
                        bottomnv.setSelectedItemId(R.id.car);break;
                    case 4:
                        bottomnv.setSelectedItemId(R.id.owner);break;

                }

            }

            @Override
            public void onPageScrollStateChanged(int state) {
                /*页面滚动状态改变*/

            }
        });

    }

    private void initBottomNV() {
        bottomnv=findViewById(R.id.bottomnv);
        /*实例化*/
        bottomnv.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
                int vpid=0;
                switch (menuItem.getItemId()){
                    case R.id.key: vpid=0;break;
                    case R.id.collect: vpid=1;break;
                    case R.id.search: vpid=2;break;
                    case R.id.car:vpid=3;break;
                   case R.id.owner:vpid=4;break;
                }
                System.out.println("--------id="+vpid);
                viewPager.setCurrentItem(vpid);
                return true;
            }
        });

    }

//    @Override
//    protected void onRestart() {
//        super.onRestart();
//        int id = getIntent().getIntExtra("id", 0);
//        if (id == 1) {
//
////                }
//        }
//    }
}
